package com.chanyachting.emall.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;

import com.chanyachting.emall.domain.Memberopertionmenu;
import com.chanyachting.emall.domain.Menugroup;
import com.chanyachting.emall.domain.Opertionmenu;

public class MemberopertionmenuBO {
	
	
	public static void main(String args[]){
		
		String menuArray="1-1,2-1,1-2,2-2,3-2,1-3";
		
		TreeMap map=MemberopertionmenuBO.disposeMenuArray(menuArray);
		
		Iterator it=map.keySet().iterator();
		
		while(it.hasNext()){
			
			String key=it.next().toString();
			
			String value=map.get(key).toString();
			
			System.out.println("--key:"+key);
			
			System.out.println("--value:"+value);
		}
		
		
		
		
	}
	
	
	/**
	 * 处理菜单数组
	 * @param menuArray
	 * @return
	 */
	private static TreeMap disposeMenuArray(String menuArray){
		
		String array []=menuArray.split(",");
		
		TreeMap map=new TreeMap();
		
		//处理菜单数组，使用 treemap 装载
		for(int i=0;i<array.length;i++){
			
			String temp=array[i];
			
			String tempArray[]=temp.split("-");
			
			String mId=tempArray[0];
			
			String mgId=tempArray[1];
			
			//如果集合中没有菜单组编号
			if(map.get(mgId)==null){
				
				map.put(mgId, mId);
				
			}else{
				
				//如果集合中已经有菜单组编号，放入新的值
				
				String value=map.get(mgId).toString();
				
				value+=","+mId;
				
				map.remove(mgId);
				
				map.put(mgId, value);
				
			}
		}
		
		return map;
		
	}
	
	
	/**
	 * 将会员菜单列表，整理成标准的菜单组列表
	 * @param memberMenuList
	 * @return
	 */
	public static List<Menugroup> getMenugroupListByMemberMenu(List<Memberopertionmenu> memberMenuList){
		
		List<Menugroup> list=new ArrayList<Menugroup>();
		
		if(memberMenuList!=null && memberMenuList.size()>0){
			
			for(int i=0;i<memberMenuList.size();i++){
				
				Memberopertionmenu mm=(Memberopertionmenu)memberMenuList.get(i);
				
				int gid=mm.getMenuGroupId();  //菜单组编号
				
				Menugroup mg=new Menugroup(gid);  //获得菜单组对象
				
				String [] marray=mm.getMenuIdArray().split(",");  //菜单编号列表
				
				List menuList=new ArrayList();
				
				for(int j=0;j<marray.length;j++){
					
					int mid=Integer.parseInt(marray[j]);
					
					Opertionmenu om=new Opertionmenu(mid);
					
					menuList.add(om);
				}
				
				mg.setMenuList(menuList); //设置菜单列表
				
				list.add(mg); //将菜单组添加到集合
				
			}
			
		}
			
		
		return list;
	}
	
	
	
	/**
	 * 检查菜单是否是会员已有菜单
	 * @param memberId
	 * @param menu
	 * @return
	 */
	public static boolean checkMemberMenu(int memberId,Opertionmenu menu){
		
		List memberMenuList=Memberopertionmenu.listByMemberId(memberId);
		
		if(memberMenuList!=null && memberMenuList.size()>0){
			
			for(int i=0;i<memberMenuList.size();i++){
				
				Memberopertionmenu mm=(Memberopertionmenu)memberMenuList.get(i);
				
				//检查菜单组
				if(mm.getMenuGroupId().equals(menu.getGroupId())){
					
					String [] marray=mm.getMenuIdArray().split(",");
					
					//检查菜单编号
					for(int j=0;j<marray.length;j++){
						
						//如果菜单组编号，菜单编号都相等，返回 true
						if(marray[j].equals(String.valueOf(menu.getId()))){
							
							return true;
						}
					}
				}
			}
			
		}
		
		return false;
	}
	
	
	public static List<Memberopertionmenu> getMemberMenuListByMenuArray(int memberId,String menuArray){
		
		//处理菜单数组
		TreeMap map=MemberopertionmenuBO.disposeMenuArray(menuArray);
		
		//组合会员菜单对象
		Iterator it=map.keySet().iterator();
		
		List<Memberopertionmenu> list=new ArrayList<Memberopertionmenu>();
		
		while(it.hasNext()){
			
			String key=it.next().toString();
			
			String value=map.get(key).toString();
			
			int mgId=Integer.parseInt(key);
			
			Memberopertionmenu mm=new Memberopertionmenu();
			
			mm.setMemberId(memberId);
			mm.setMenuGroupId(mgId);
			mm.setMenuIdArray(value);
			
			list.add(mm);
		
		}
		
		return list;
	}
	
	
	
	public static boolean saveMemberopertionmenuList(List<Memberopertionmenu> list){
		
		if(list==null || list.size()<1) return false;
		
		Memberopertionmenu menu=null;
		
		for(int i=0;i<list.size();i++){
			
			menu=list.get(i);
			
			menu.save();
		}
		
		return true;
	}

}
